package com.waner.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.waner.reggie.common.BaseContext;
import com.waner.reggie.common.R;
import com.waner.reggie.entity.AddressBook;
import com.waner.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 保存地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook, HttpSession session){
        // 保存地址
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.success("地址保存成功..");
    }

    /**
     * 根据userId查询地址
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(){
        log.info("根据用户id查询");
        // 构建查询器：select * from address_book where user_id = ?
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId()).orderByDesc(AddressBook::getUpdateTime);

        List<AddressBook> list = addressBookService.list(queryWrapper);

        return R.success(list);
    }

    @PutMapping("/default")
    public R<String> setDefault(@RequestBody AddressBook addressBook){
        // 将此客户所有对应的地址更改为非默认地址： update address_book set is_default = 0 where user_id = ?
        // 构造对应的查询器
        LambdaUpdateWrapper<AddressBook> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        queryWrapper.set(AddressBook::getIsDefault,0);

        addressBookService.update(queryWrapper);

        // 根据地址id将更新地址为默认地址： update address_book set isDefault = 0 where id = ?
        AddressBook updateAddressBook = addressBookService.getById(addressBook.getId());
        updateAddressBook.setIsDefault(1);
        addressBookService.updateById(updateAddressBook);

        return R.success("更新成功");
    }

    /**
     * 根据地址的id查询地址信息
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> getById(@PathVariable String id){
        log.info("根据地址id查询地址信息...");
        AddressBook addressBook = addressBookService.getById(id);
        if(addressBook == null){
            return R.error("没有查询到对应的地址...");
        }
        return R.success(addressBook);
    }

    /**
     * 根据id更新地址信息
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){
        log.info("更新地址信息..");
        addressBookService.updateById(addressBook);
        return R.success("更新成功");
    }

    /**
     * 根据id删除对应的地址信息
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long ids){
        log.info("删除的操作...");

        // select * from address_book where id = ?
        addressBookService.removeById(ids);

        return R.success("删除成功！");
    }

    @GetMapping("/default")
    public R<AddressBook> getDefault(){
        log.info("获取默认地址...");
        // select * from address_book where isDefault = 1
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getIsDefault,1)
                .eq(AddressBook::getUserId,BaseContext.getCurrentId());

        AddressBook addressBook = addressBookService.getOne(wrapper);

        if(addressBook == null){
            return R.error("没有找到对应的默认地址...");
        }

        return R.success(addressBook);
    }

}
